﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DevExpress.Web.Mvc;
using DevExpress.Data.Filtering;
using DevExpress.Data.Linq;
using DevExpress.Data.Linq.Helpers;

namespace GridViewCustomBinding.Model {
    public static class GridViewCustomBindingHelper {
        static IQueryable<Car> Model { get { return GridModel.DB.AsQueryable<Car>(); } }

        public static void GetDataRowCount(GridViewCustomBindingGetDataRowCountArgs e) {
            e.DataRowCount = Model.ApplyFilter(e.FilterExpression).Count();
        }

        public static void GetData(GridViewCustomBindingGetDataArgs e) {
            e.Data = Model
                .Skip(e.StartDataRowIndex)
                .Take(e.DataRowCount)
                .ToList();
        }
    }

    public static class GridViewCustomOperationDataHelper {
        static ICriteriaToExpressionConverter Converter { get { return new CriteriaToExpressionConverter(); } }

        public static IQueryable ApplyFilter(this IQueryable query, string filterExpression) {
            return query.AppendWhere(Converter, CriteriaOperator.Parse(filterExpression));
        }
    }
}